﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
using MensUnoShop.Models;
using System.Xml.Linq;

namespace MensUnoShop.Helpers
{
    public static class ADHelper
    {
        public static string GetAdIndex(this HtmlHelper helper, XElement xindex)
        {
           StringBuilder sb = new StringBuilder();
            string sbflash = string.Empty;
           sbflash = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"650\" height=\"380\">";
            sbflash+= "<param name=\"movie\" value=\"{0}\" />";
            sbflash+= "<param name=\"quality\" value=\"high\" />";
            sbflash+= "<embed src=\"{1}\" quality=\"high\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"650\" height=\"380\"></embed>";
            sbflash += "</object>";

            string sbimg = string.Empty;
            sbimg = "<a href=\"{0}\" _target=\"{1}\"><img href=\"{2}\" border=\"0\" width=\"650\" height=\"380\" /></a>";
            try{
            XElement xad = xindex.Element("ad");
            string img = xad.Element("img").Value;
           
                if (!string.IsNullOrEmpty(img))
                {
                    string[] ss = img.Split('.');
                    if (ss.Last().ToLower() == "swf")
                    {
                        sbflash = string.Format(sbflash, img, img);
                        sb.Append(sbflash);
                    }
                    else
                    {
                        sbimg = string.Format(sbimg, xad.Element("link").Value, xad.Attribute("isopen").Value == "1" ? "_blank" : string.Empty, img);
                        sb.Append(sbimg);
                    }
                }
            }
            catch { }

            return sb.ToString();
        }
    }
}